package com.axonactive.yolo.validation;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

import com.axonactive.yolo.dto.ResetPasswordParameters;
import com.axonactive.yolo.util.Utils;

@Component
public class ResetPasswordValidator implements Validator {
	
	private static final Logger LOGGER = Logger.getLogger(ResetPasswordValidator.class);

	@Override
	public boolean supports(Class<?> clazz) {
		return ResetPasswordParameters.class.isAssignableFrom(clazz);
	}

	@Override
	public void validate(Object target, Errors errors) {
		ResetPasswordParameters resetPasswordParameters = (ResetPasswordParameters) target;
		LOGGER.info("pass here !!!!!!!!!!! ---> " +resetPasswordParameters.getNewPassword()+resetPasswordParameters.getConfirmPassword());
		ValidationUtils.rejectIfEmpty(errors, "newPassword",
				"password.notnull");
		ValidationUtils.rejectIfEmpty(errors, "confirmPassword",
				"confirm.password.notnull");

		if (!"".equals(resetPasswordParameters.getNewPassword())
				&& !Utils.isValidPassword(resetPasswordParameters
						.getNewPassword())) {
			errors.rejectValue("password", "pass.format");
		}

		if (Utils.isValidPassword(resetPasswordParameters.getNewPassword()) && !"".equals(resetPasswordParameters.getConfirmPassword())){
			if(!resetPasswordParameters.getConfirmPassword().trim().equals(resetPasswordParameters.getNewPassword().trim())) {
				errors.rejectValue("confirmPassword", "confirm.password.notmatch");
			}
		}
	}

}
